package com.liehacker.android.mapservice;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.liehacker.android.R;
import com.liehacker.android.util.DebugLog;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class C24_MapView extends MapActivity{
	
	MapView mMapView;
	MapController mMapCtrl;
	
	LocationManager mLocMan;
	String mStrProvider;
	
	static final String TAG = "C24_MapView";
	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.c24_mapview);
		
		mMapView = (MapView)findViewById(R.id.c24_mapview);
		mMapCtrl = mMapView.getController(); 
		
		mMapCtrl.setZoom(16);
		mMapView.setBuiltInZoomControls(true);
		
		GeoPoint geo = new GeoPoint(37881311, 127729968);
		mMapCtrl.setCenter(geo);
		
		mLocMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		mStrProvider = mLocMan.getBestProvider(new Criteria(), true);
		
		DebugLog.v(TAG, "onCreate");
			
	}
	
	
	public boolean onCreateOptionsMenu(Menu menu) {
			super.onCreateOptionsMenu(menu);
			
			menu.add(0,1,0,"지도");
			menu.add(0,2,0,"위성");
			menu.add(0,3,0,"거리");
			menu.add(0,4,0,"교통");
			menu.add(0,5,0,"내위치");
			menu.add(0,6,0,"춘천시청");
			menu.add(0,7,0,"강원도청");
			menu.add(0,8,0,"63빌딩");
			
			return true;
	}

	// return true; 
	public boolean onOptionsItemSelected(MenuItem menu) {
		
		switch(menu.getItemId()) {
		case 1:
			mMapView.setSatellite(false);
			FnStopGps();
			return true;
		case 2:
			mMapView.setSatellite(true);
			FnStopGps();
			return true;
		case 3:
			mMapView.setStreetView(true);
			FnStopGps();
			return true;
		case 4:
			mMapView.setTraffic(true);
			FnStopGps();
			return true;
		case 5:
			mLocMan.requestLocationUpdates(mStrProvider, 3000, 10, mListener);
			return true;
		case 6:
			mMapCtrl.animateTo(new GeoPoint(37881311, 127729968));
			FnStopGps();
			return true;
		case 7:
			mMapCtrl.animateTo(new GeoPoint(37885269, 127729592));
			FnStopGps();
			return true;
		case 8:
			GeoPoint gp = new GeoPoint((int)(37.519576 * 1000000.0) , (int)(126.940245*1000000.0));
			mMapView.getController().setCenter(gp);
			FnStopGps();
			return true;			
		}
		
		return false;		
	}
	
	protected void FnStopGps() {
		mLocMan.removeUpdates(mListener);
	}
	
	LocationListener mListener = new LocationListener() {

		@Override
		public void onLocationChanged(Location arg0) {
			// TODO Auto-generated method stub
			mMapCtrl.animateTo( new GeoPoint( (int)(arg0.getLatitude()*1000000.0), (int)(arg0.getLongitude()*1000000.0)) );
		}

		@Override
		public void onProviderDisabled(String arg0) {
			// TODO Auto-generated method stub
			Toast.makeText(getBaseContext(), "사용 불능", Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onProviderEnabled(String arg0) {
			// TODO Auto-generated method stub
			Toast.makeText(getBaseContext(), "사용 가능", Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle arg2) {
			// TODO Auto-generated method stub
			String strStatus = "";
			switch(status) {
			case LocationProvider.OUT_OF_SERVICE :
				strStatus = "범위 벗어남";
				break;
			case LocationProvider.AVAILABLE :
				strStatus = "사용 가능";
				break;
			case LocationProvider.TEMPORARILY_UNAVAILABLE :
				strStatus = "일시적 불능";
				break;
			}
			
			Toast.makeText(getBaseContext(), provider + "상태 변경 : " + strStatus, Toast.LENGTH_SHORT).show();
		}
		
	};
	
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
}
